﻿/*~~~~~~~~~~~~~~~~~~Not Literal Constructor~~~~~~~~~~~~~~~~~~*/

/*Constructor for task objects*/
var Task = function (name/*This is the title of the task*/) {
	this.name = name;
	this.isDone = false;
	this.startTime = Date.now();
};

/*The array to hold tasks*/
var tasks = [];
var hArray = [];
/*Adds Tasks to the Tasks Array*/
var addTask = function () {
	/*Access Element*/
	var e = document.getElementById('taskName');
	/*New Object*//*Extract Value*/
	var t = new Task(e.value);
	/*Push to array*/
	tasks.push(t);
	/*Clear Input*/
	e.value = "";
	/*Write to table*/
	tableWriter();
};
/*Write tasks to table*/
var tableWriter = function () {
	var h = "<table class='table table-hover'>";
	h += "<tr><th>Task</th></tr>";
	/*for (var i = 0; i < tasks.length; i++)*/
	for (var i in tasks) {
		var time = "";
		if (tasks[i].isDone) {
			h += "<tr class='warning' onclick='toggleDone(" + i + ")'>";
			time = "<td>" + (tasks[i].endTime - tasks[i].startTime) / 1000 + "</td>";
		} else {
			h += "<tr onclick='toggleDone(" + i + ")'>";
		}
		h += "<td>" + tasks[i].name + "</td>";
		h += "<td>" + new Date(tasks[i].startTime).toLocaleString() + "</td>";
		if (time) {
			h += time;
			h += "<td class='btn btn-danger' onclick= 'deleteTask(" + i + ")'> Delete</td>";
		}
		h += "</tr>";
	}
	h += "</table>";
	document.getElementById("tableHolder").innerHTML = h;
};
/*Marks tasks complete*/
var toggleDone = function (/*Index of element*/index) {
	/*Set object task done*/
	tasks[index].isDone = !tasks[index].isDone;
	/*Finish Time*/
	tasks[index].endTime = Date.now();
	/*Write table*/
	tableWriter();
};

/*Sort by done*/
var sortDone = function () { };

/*Clear finished*/
var clearDone = function () {	
	for (var i in tasks) {
		if (tasks[i].isDone) {
			hArray.push(tasks[i]);
		}
	}
	tasks = hArray;
	tableWriter();
};

/*Delete single Task*/
var deleteTask = function () {
	for (var i in tasks) {
		if (tasks[i].isDone) {
			hArray.push(tasks[i]);
		}
	}
	tasks = hArray;
	tableWriter();
};
